package com.xky.ui.module.register;

import com.xky.ui.module.login.Login;
import com.xky.utils.DBUtils;

import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Register extends JFrame {

    public Register() {
        setTitle("注册界面");
        setSize(450, 450);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents();
        setVisible(true);
    }

        void initComponents() {
           setLayout(new BorderLayout());
            JLabel label1 = new JLabel("用户注册",SwingConstants.CENTER);
            label1.setBounds(100, 20, 200, 30);
            label1.setFont(new java.awt.Font("华文行楷", Font.PLAIN, 30));
            add(label1, BorderLayout.NORTH);


            //表单面板
            JPanel jPanel = new JPanel(new GridBagLayout());
            jPanel.setBackground(new Color(255, 182, 193));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(5,5,5,5);

            jPanel.setOpaque(false); // 设置主面板为透明


            Font font = new Font("微软雅黑", Font.PLAIN, 20);

            JLabel label2 = new JLabel("用户名：");
            label2.setFont(font);
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weighty = 0;
            gbc.insets = new Insets(10,50,10,10);
            gbc.fill = GridBagConstraints.NONE;
            jPanel.add(label2,gbc);


            JTextField usernameField = new JTextField(20);
            usernameField.setPreferredSize(new Dimension(400,35));
            gbc.gridx = 1;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.gridwidth = 2;
            gbc.insets = new Insets(10,0,10,50);
            gbc.fill = GridBagConstraints.HORIZONTAL;
            jPanel.add(usernameField,gbc);

            JLabel jLabel1 = new JLabel("密码：");
            jLabel1.setFont(font);
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.insets = new Insets(10,70,10,10);
            gbc.fill = GridBagConstraints.NONE;
            jPanel.add(jLabel1,gbc);


            JPasswordField passwordField = new JPasswordField(20);
            passwordField.setPreferredSize(new Dimension(400,35));
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.gridwidth = 2; // 输入框占据一个网格单元
            gbc.insets = new Insets(10,0,10,50);
            gbc.fill = GridBagConstraints.HORIZONTAL;
            jPanel.add(passwordField,gbc);


            JLabel jLabel2 = new JLabel("确认密码：");
            jLabel2.setFont(font);
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.insets = new Insets(10,30,10,10);
            gbc.fill = GridBagConstraints.NONE;
            jPanel.add(jLabel2,gbc);


            JPasswordField password1Field = new JPasswordField(20);
            password1Field.setPreferredSize(new Dimension(400,35));
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.gridwidth = 2; // 输入框占据一个网格单元
            gbc.insets = new Insets(10,0,10,50);
            gbc.fill = GridBagConstraints.HORIZONTAL;
            jPanel.add(password1Field,gbc);

            JLabel jLabel3 = new JLabel("邮箱：");
            jLabel3.setFont(font);
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.insets = new Insets(10,70,10,10);
            gbc.fill = GridBagConstraints.NONE;
            jPanel.add(jLabel3,gbc);

            JTextField emailField = new JTextField(20);
            emailField.setPreferredSize(new Dimension(400,35));
            gbc.gridx = 1;
            gbc.weightx = 1;
            gbc.weighty = 0;
            gbc.gridwidth = 2; // 输入框占据一个网格单元
            gbc.insets = new Insets(10,0,10,50);
            gbc.fill = GridBagConstraints.HORIZONTAL;
            jPanel.add(emailField,gbc);

            //角色提示
            JLabel jLabel = new JLabel("你当前注册的角色是管理员");
            jLabel.setFont(new Font("华文行楷",Font.PLAIN,15));
            jLabel.setForeground(Color.LIGHT_GRAY);
            gbc.gridx = 0;
            gbc.gridy = 4;
            gbc.insets = new Insets(5,30,10,10);
            jPanel.add(jLabel,gbc);

            //按钮
            JButton jButton = new JButton("注册");
            jButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
            jButton.setForeground(Color.WHITE);
            jButton.setBackground(new Color(255, 182, 193)); // 柔和的粉色
            jButton.setPreferredSize(new Dimension(100, 35));
            jButton.setFocusPainted(false);
            jButton.setBorderPainted(false); // 禁用默认边框
            jButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
            gbc.gridx = 0;
            gbc.gridy = 5;
            gbc.insets = new Insets(40,120,10,10);
            gbc.fill = GridBagConstraints.NONE;
            String EMAIL = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$";
            jButton.addActionListener(e->{
                String username = usernameField.getText().trim();
                String password = new String(passwordField.getPassword()).trim();
                String password1 = new String(password1Field.getPassword()).trim();
                String email = emailField.getText().trim();

                if (username.isEmpty() || password.isEmpty() || password1.isEmpty() || email.isEmpty()){
                    JOptionPane.showMessageDialog(Register.this,"注册信息不能为空，请输入：","注册提示",JOptionPane.WARNING_MESSAGE);
                }else if (!password.equals(password1)){
                    JOptionPane.showMessageDialog(Register.this,"你填写的密码不一致，请重新输入，","错误提示",JOptionPane.WARNING_MESSAGE);
                    passwordField.setText("");
                    password1Field.setText("");
                    }else if (!email.matches(EMAIL)){
                        JOptionPane.showMessageDialog(Register.this,"邮箱格式不对，请重新输入","错误提示",JOptionPane.ERROR_MESSAGE);
                        emailField.setText("");
                    }else if (addUser(username,password,email)){
                    JOptionPane.showMessageDialog(Register.this,"恭喜你，注册成功！","成功提示",JOptionPane.INFORMATION_MESSAGE);
                    dispose();
                    new Login();
                        }else {
                    JOptionPane.showMessageDialog(Register.this,"注册失败？","失败提示",JOptionPane.ERROR_MESSAGE);

                }

            });

            jPanel.add(jButton,gbc);

            //按钮
            JButton jButton1 = new JButton("返回");
            jButton1.setFont(new Font("微软雅黑", Font.BOLD, 14));
            jButton1.setForeground(Color.WHITE);
            jButton1.setBackground(new Color(135, 206, 250)); // 淡蓝色
            jButton1.setPreferredSize(new Dimension(100, 35));
            jButton1.setFocusPainted(false);
            jButton1.setBorderPainted(false); // 禁用默认边框
            jButton1.setCursor(new Cursor(Cursor.HAND_CURSOR));
            gbc.gridx = 1;
            gbc.gridy = 5;
            gbc.insets = new Insets(40,120,10,10);
            gbc.fill = GridBagConstraints.NONE;

            jButton1.addActionListener(e->{
                dispose();
                new Login();
            });

            jPanel.add(jButton1,gbc);
            add(jPanel, BorderLayout.CENTER);
        }

        //添加用户信息
    public boolean addUser(String username, String password, String email){
        Connection connection;
        PreparedStatement preparedStatement = null;

        connection =  DBUtils.getConnection();
        try {
           preparedStatement = connection.prepareStatement("insert into login(username, password, email) values (?,?,?)");
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            preparedStatement.setString(3,email);
            int i = preparedStatement.executeUpdate();

            if (i>0){
                System.out.println("受影响的行数为"+i+",添加成功！");
            }else {
                System.out.println("受影响的行数为"+i+",添加失败！");
            }

            return true;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtils.closeAll(connection,preparedStatement,null);
        }
    }

    public static void main(String[] args) throws UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(Register::new);
    }
}
